package restaurant.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

/**
 * This panel displays the menu.
 * 
 * @author Odessa
 */
public class ViewMenu extends javax.swing.JPanel
{

	private JPanel thisPanel = this;
	protected int id;
	protected String name;
	protected String desc;
	protected double price;
	protected int prepTime;
	protected String course;
	protected boolean veggie;



	public static void main(String[] args)
	{
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setLayout(new BorderLayout());
		frame.getContentPane().add(new ViewMenu(), BorderLayout.CENTER);
		frame.pack();
		frame.setVisible(true);
	}



	/** Creates new form ViewMenu */
	public ViewMenu() {
		initComponents();

		TableColumnModel model = menuTable.getColumnModel();
		model.removeColumn(menuTable.getColumn("Item number"));

		ListSelectionModel selectionModel = menuTable.getSelectionModel();
		selectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		selectionModel.addListSelectionListener(new MenuRowListener(this));
	}



	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{

		scrollPane = new JScrollPane();
		menuTable = new JTable();
		buttonPanel = new JPanel();
		availabilityButton = new JButton();
		editButton = new JButton();
		okButton = new JButton();

		menuTable.setModel(new DefaultTableModel(
				new Object[][] {
						{ new Integer(1), "Orange Juice", "Big glass of OJ...",
								"Drinks", new Double(2.5), new Integer(1),
								new Boolean(false), new Double(3.5) },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null },
						{ null, null, null, null, null, null,
								new Boolean(false), null } }, new String[] {
						"Item number", "Name", "Description", "Course",
						"Regular price", "Preparation time", "Vegetarian",
						"Rating" }) {
			Class[] types = new Class[] { Integer.class, String.class,
					String.class, String.class, Double.class, Integer.class,
					Boolean.class, Double.class };
			boolean[] canEdit = new boolean[] { false, false, false, false,
					false, false, false, false };



			public Class getColumnClass(int columnIndex)
			{
				return types[columnIndex];
			}



			public boolean isCellEditable(int rowIndex, int columnIndex)
			{
				return canEdit[columnIndex];
			}
		});
		scrollPane.setViewportView(menuTable);

		buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

		availabilityButton.setText("Add availability");
		availabilityButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt)
			{
				availabilityButtonActionPerformed(evt);
			}
		});
		buttonPanel.add(availabilityButton);

		editButton.setText("Edit");
		editButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt)
			{
				editButtonActionPerformed(evt);
			}
		});
		buttonPanel.add(editButton);

		okButton.setText("OK");
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt)
			{
				okButtonActionPerformed(evt);
			}
		});
		buttonPanel.add(okButton);

		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING)
												.addComponent(
														buttonPanel,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														scrollPane,
														GroupLayout.PREFERRED_SIZE,
														800,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap(30, Short.MAX_VALUE)));
		layout.setVerticalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(scrollPane,
										GroupLayout.PREFERRED_SIZE, 275,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED,
										18, Short.MAX_VALUE)
								.addComponent(buttonPanel,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)));
	}// </editor-fold>



	private void editButtonActionPerformed(ActionEvent evt)
	{
		Frame frame;

		if (desc != null) {
			frame = new Frame(new MenuItemPanel(id, name, desc, price,
					prepTime, course, veggie));
			frame.run();
		}
		else {
			frame = new Frame(new MenuItemPanel());
			frame.run();
		}
	}



	private void okButtonActionPerformed(ActionEvent evt)
	{
		JFrame frame = (JFrame) SwingUtilities.getRoot(thisPanel);
		frame.dispose();
	}



	private void availabilityButtonActionPerformed(ActionEvent evt)
	{
		if (desc != null) {
			JFrame frame = (JFrame) SwingUtilities.getRoot(thisPanel);
			ViewMenuDialog availDialog = new ViewMenuDialog(frame,
					"Enter item availibility below", "Item availability",
					ViewMenuDialog.AVAIL);
			availDialog.pack();
			availDialog.setLocationRelativeTo(frame);
			availDialog.setVisible(true);

			String avail = availDialog.getValidatedText();

			Restaurant.actions.setAvailability(id, Integer.parseInt(avail));
		}
	}

	// Variables declaration - do not modify
	private JButton availabilityButton;
	private JPanel buttonPanel;
	private JButton editButton;
	protected JTable menuTable;
	private JButton okButton;
	private JScrollPane scrollPane;
	// End of variables declaration
}

class MenuRowListener implements ListSelectionListener
{

	ViewMenu row;
	JTable table;



	public MenuRowListener(ViewMenu vm) {
		row = vm;
		table = row.menuTable;
	}



	public void valueChanged(ListSelectionEvent e)
	{
		if (!e.getValueIsAdjusting()) {
			ListSelectionModel model = table.getSelectionModel();
			int index = model.getLeadSelectionIndex();
			getRowValues(index);
		}
	}



	private void getRowValues(int rowIndex)
	{
		try {
			row.id = (Integer) table.getModel().getValueAt(rowIndex, 0);
			row.name = (String) table.getValueAt(rowIndex, 0);
			row.desc = (String) table.getValueAt(rowIndex, 1);
			row.course = (String) table.getValueAt(rowIndex, 2);
			row.price = (Double) table.getValueAt(rowIndex, 3);
			row.prepTime = (Integer) table.getValueAt(rowIndex, 4);
			row.veggie = (Boolean) table.getValueAt(rowIndex, 5);

			System.out.println(row.id + ", " + row.name + ", " + row.desc +
					", " + row.course + ", " + row.price + ", " + row.prepTime +
					", " + row.veggie);
		}
		catch (NullPointerException npe) {
			row.id = 0;
			row.name = null;
			row.desc = null;
			row.course = null;
			row.price = 0;
			row.prepTime = 0;
			row.veggie = false;
		}

		// System.out.println(row.id + ", " +row.desc + ", " +row.discount +
		// ", " + row.startDate + ", " + row.endDate);
	}
}
